package com.db.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.db.core.dto.PageDto;
import com.db.core.vo.PageResult;
import com.db.deviceType.dto.DeviceTypePageDto;
import com.db.deviceType.entity.DeviceType;
import com.db.product.dto.ProductPageDto;
import com.db.product.entity.Product;
import com.db.product.mapper.ProductMapper;
import com.db.product.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 产品表 服务实现类
 * </p>
 *
 * @author vodka
 * @since 2023-10-27
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Override
    public PageResult page(ProductPageDto productPageDto) {
        //获取分页信息
        Integer size = productPageDto.getSize();
        Integer now = productPageDto.getNow();
        //查询总记录数
        Long total = baseMapper.selectCount(new QueryWrapper<Product>());
        //计算总页数
        long pages = (total + size - 1) / size;
        //开始查询
        long start = (now - 1) * size;
        List<Product> page = baseMapper.page(productPageDto, start);
        //将数据封装到pageBean
        PageResult<Product> pageResult = new PageResult<>();
        pageResult.setTotal(total);
        pageResult.setPages(pages);
        pageResult.setList(page);
        return pageResult;
    }


}
